package com.qianbei.user.withdrawal;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qianbei.R;

import java.util.List;

/**
 * Created by GaoQian on 2017/11/2.
 */

public class WithDrawalListAdapter extends RecyclerView.Adapter<WithDrawalListAdapter.MainViewHolder> {


    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }
    public interface OnItemClickLitener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }
    private Context mContext;
    private List<WithDrawalBean.DataBean.QuotaBean> quotaBeans;

    public WithDrawalListAdapter(Context mContext, List<WithDrawalBean.DataBean.QuotaBean> quotaBeans) {
        this.mContext = mContext;
        this.quotaBeans = quotaBeans;
    }


    /**
     * 绑定item布局
     *
     * @param parent
     * @param pos
     * @return
     */
    @Override
    public MainViewHolder onCreateViewHolder(ViewGroup parent, int pos) {
        return new MainViewHolder((ViewGroup) LayoutInflater.from(parent.getContext()).inflate(R.layout.kh_item_withdrawal, parent, false));
    }

    /**
     * 绑定数据到控件
     *
     * @param holder
     * @param pos
     */
    @Override
    public void onBindViewHolder(final MainViewHolder holder, int pos) {
        int money = quotaBeans.get(pos).getMoney();
        int num = quotaBeans.get(pos).getNum();

        holder.bind(money, num);

        // 如果设置了回调，则设置点击事件
        if (mOnItemClickLitener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder.itemView, pos);

                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemLongClick(holder.itemView, pos);
                    return false;
                }
            });
        }
    }

    /**
     * 返回列表条数
     *
     * @return
     */
    @Override
    public int getItemCount() {
        return quotaBeans.size();
    }

    /**
     * 实例化控件等操作
     */
    public class MainViewHolder extends RecyclerView.ViewHolder {


        public final TextView mTvWdNum;
        private final TextView mTvWdMoney;
        private final LinearLayout mLlWdBg;

        // 实例化
        public MainViewHolder(ViewGroup itemView) {
            super(itemView);

            mTvWdNum = ((TextView) itemView.findViewById(R.id.tv_wd_num));
            mTvWdMoney = (TextView) itemView.findViewById(R.id.tv_wd_money);
            mLlWdBg = (LinearLayout) itemView.findViewById(R.id.ll_wd_bg);

        }

        public void bind(int money, int num) {
            mTvWdNum.setText("库存" + num + "个");
            mTvWdMoney.setText(money + "元");
            if (num == 0) {
                mLlWdBg.setBackgroundResource(R.drawable.roll_wd_gray);
                mTvWdNum.setTextColor(Color.WHITE);
                mTvWdMoney.setTextColor(Color.WHITE);
            }
        }

    }
}
